﻿using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using DBUtility;
using gfLibrary;
using IYAKASAPortal.Services;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace IYAKASAPortal.PrintAll
{
    public partial class PrintAJ : System.Web.UI.Page
    {

        private UserInfo userInfo { get; set; }
        private ReportDocument document;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                {
                    string userName = Session["UserName"].ToString();
                    userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
                }
                else
                {
                    gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
                }

                if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
                {
                    BindReportData(Request["UniqueKey"].ToString());
                }
                else
                {
                    gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                BindReportData(Request["UniqueKey"].ToString());
            }
        }

        private void BindReportData(string strKey)
        {
            string strPath = Server.MapPath("~/rpt/CrystalReportAJ.rpt");
            bool bSingle = chkSingle.Checked;

            SetCRDataSource setData = new SetCRDataSource();
            document = setData.BindAJReportEx(strKey, strPath,bSingle);
            CRAJ.ReportSource = document;
            CRAJ.DataBind();
            CRAJ.RefreshReport();

            CONST_DEFINE_WEB_BLL bll = new CONST_DEFINE_WEB_BLL();
            var constInfos = bll.GetModelList("CategoryID=5 order by SeqNumber");

            ListItem oItem;
            chkSuggest.Items.Clear();
            foreach (var v in constInfos)
            {
                oItem = new ListItem(){ Text=v.Remark, Value = v.ID.ToString()};
                chkSuggest.Items.Add(oItem);
            }

            BGD_SUGGEST_BLL bgd_bll = new BGD_SUGGEST_BLL();
            RESULT_VEHICLE_INFO_BLL vehicleInfo = new RESULT_VEHICLE_INFO_BLL();
            string strJCLSH = vehicleInfo.GetModelList("ID=" + strKey.ToString())[0].JCLSH;

            var bgd = bgd_bll.GetModelList("JCLSH='" + strJCLSH + "' and SingleFlag='" + bSingle + "'");
            if (bgd.Length > 0)
            {
                if (!string.IsNullOrEmpty(bgd[0].SuggestItems))
                {
                    string strValues = bgd[0].SuggestItems;
                    CONST_DEFINE_WEB_BLL web_bll = new CONST_DEFINE_WEB_BLL();
                    strValues = strValues.Substring(0, strValues.Length - 1);
                    if (strValues.Contains(","))
                    {
                        string[] lst = strValues.Split(',');

                        for (int i = 0; i < chkSuggest.Items.Count; i++)
                        {
                            foreach (var v in lst)
                            {
                                if(chkSuggest.Items[i].Value == v)
                                {
                                    chkSuggest.Items[i].Selected = true;
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < chkSuggest.Items.Count; i++)
                        {
                            chkSuggest.Items[i].Selected = (chkSuggest.Items[i].Value == strValues);
                        }
                    }
                }
            }

        }

        protected void btnRG_Click(object sender, EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                Response.Redirect("PrintAJRG.aspx?UniqueKey=" + Request["UniqueKey"].ToString());
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
        }

        protected void btnYQ_Click(object sender, EventArgs e)
        {
            RESULT_VEHICLE_INFO_BLL BLL = new RESULT_VEHICLE_INFO_BLL();
            var INFO = BLL.GetModelList("ID=" + Request["UniqueKey"].ToString());
            
            string CLZLDH = INFO[0].CLZLDH.Trim();
            if (CLZLDH != "")
            {
                if (CLZLDH.Substring(0, 1) == "M" || CLZLDH=="N11")
             {

                if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
                {
                    Response.Redirect("PrintAJSLCMTC.aspx?UniqueKey=" + Request["UniqueKey"].ToString());
                }
                else
                {
                    gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
                }                      
            }
             else

           if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                Response.Redirect("PrintAJYQ.aspx?UniqueKey=" + Request["UniqueKey"].ToString());
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
            }
           
                                      
        }

        protected void btnAJ_Click(object sender, EventArgs e)
        {
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                BindReportData(Request["UniqueKey"].ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try{
            string strJCLSH = "";
            if (Request["UniqueKey"] != null && Request["UniqueKey"].ToString() != "")
            {
                RESULT_VEHICLE_INFO_BLL vehicleInfo = new RESULT_VEHICLE_INFO_BLL();
                strJCLSH = vehicleInfo.GetModelList("ID="+Request["UniqueKey"].ToString())[0].JCLSH;
            }
            string strAJ_SUG = "";
            for (int i = 0; i < chkSuggest.Items.Count; i++)
            {
                if (chkSuggest.Items[i].Selected)
                {
                    strAJ_SUG += chkSuggest.Items[i].Value + ",";
                }
            }


            bool bSingle = chkSingle.Checked;
            BGD_SUGGEST_BLL bll = new BGD_SUGGEST_BLL();
            var bgd = bll.GetModelList("JCLSH='" + strJCLSH + "' and SingleFlag='" + bSingle + "'");

            BGD_SUGGEST sug =new BGD_SUGGEST();
            if (bgd.Length > 0)
            {
                sug = bgd[0];
                sug.SuggestItems = strAJ_SUG;

                bool bUpdate = bll.Update(sug, sug.ID);
                if (bUpdate)
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存成功！");
                    BindReportData(Request["UniqueKey"].ToString());
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存失败！");
                    return;
                }
            }
            else
            {
                sug.ID = bll.GetMax_ID();
                sug.SuggestItems = strAJ_SUG;
                sug.SingleFlag = bSingle;
                sug.JCLSH = strJCLSH;
                if (bll.Insert(sug))
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存成功！");
                    BindReportData(Request["UniqueKey"].ToString());
                }
                else
                {
                    gfAjaxJs.SetAlertMsg(this.UpdatePanel1, "保存失败！");
                    return;
                }
            }

            }catch(Exception ex)
            {
            }
        }

        protected void chkSingle_CheckedChanged(object sender, EventArgs e)
        {
            btnAJ_Click(sender, e);
        }

        protected override void OnUnload(EventArgs e)
        {
            document.Dispose();
            base.OnUnload(e);
        }
    }
}